package info.cs52.fes.action;

import info.cs52.fes.dao.UserDAO;
import info.cs52.fes.data.User;

import javax.servlet.http.HttpServletRequest;

public class SessionUtils {

	public static boolean isLoggedIn(HttpServletRequest req) {
		Boolean loggedIn = (Boolean) req.getSession().getAttribute("loggedIn");
		return loggedIn == null ? false : loggedIn;
	}
	
	public static User getUser(HttpServletRequest req) {
		Long id = (Long) req.getSession().getAttribute("userId");
		if (id == null) {
			return null;
		}
		return UserDAO.find(id);
	}
	
	public static void login(HttpServletRequest req, User user) {
		req.getSession().setAttribute("userId", user.getId());
		req.getSession().setAttribute("loggedIn", true);
	}
	
	public static void logout(HttpServletRequest req) {
		req.getSession().setAttribute("loggedIn", false);
	}
	
}
